using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using System.Linq.Expressions;
using ACME.Northwind.Service.Interfaces;

namespace ACME.Northwind.Service.IOCExtensions
{
	/// <summary>
	/// This is an extension class for Employee DTO objects and lists
	/// </summary>
	public static partial class EmployeeExtension
	{
		#region RunSelect
		/// <summary>
		/// Select all objects from store.
		/// </summary>
		public static void RunSelect(this List<EmployeeDTO> list)
		{
			var proxy = ProxyFactory<EmployeeDTO>.GetProxy();
			list.AddRange(proxy.RunSelect());
		}
		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a paged result set.
		/// </summary>
		public static PagedQueryResults<EmployeeDTO> RunSelect(this List<EmployeeDTO> list, int page, int pageSize, bool ascending, string sortColumn, string linq)
		{
			var proxy = ProxyFactory<EmployeeDTO>.GetProxy();
			PagedQueryResults<EmployeeDTO> retval = proxy.RunSelect(page, pageSize, ascending, sortColumn, linq);
			if (retval.GridData != null)
				list.AddRange(retval.GridData);
			return retval;
		}

		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a result set.
		/// </summary>
		public static void RunSelect(this List<EmployeeDTO> list, string linq)
		{
			var proxy = ProxyFactory<EmployeeDTO>.GetProxy();
			list.AddRange(proxy.RunSelect(linq));
		}

		#endregion

		#region Persist

		/// <summary>
		/// Persists this object to store.
		/// </summary>
		public static void Persist(this EmployeeDTO employee)
		{
			var proxy = ProxyFactory<EmployeeDTO>.GetProxy();
			proxy.Persist(employee);
		}

		/// <summary>
		/// Persists this collection to store.
		/// </summary>
		public static void Persist(this List<EmployeeDTO> employeeList)
		{
			var proxy = ProxyFactory<EmployeeDTO>.GetProxy();
			proxy.Persist(employeeList);
		}

		#endregion

		#region Employee SelectUsingPK

		/// <summary>
		/// Select a single object from this collection by its primary key.
		/// </summary>
		public static bool SelectByPrimaryKey(this EmployeeDTO employee, int employeeid)
		{
			var proxy = ProxyFactory<EmployeeDTO>.GetProxy();
			IEmployeeService employeeService = (IEmployeeService)proxy;
			var item = employeeService.SelectByPrimaryKey(employeeid);
			if (item == null) return false;
			CopyObject(item, employee);
			return true;
		}

		#endregion

		#region Employee Select By Foreign Key
		/// <summary>
		/// Get the related 'Employee' DTO object
		/// </summary>
		public static EmployeeDTO GetEmployee(this EmployeeDTO item)
		{
			var proxy = ProxyFactory<EmployeeDTO>.GetProxy();
			IEmployeeService service = (IEmployeeService)proxy;
			return service.GetEmployeeItem(item);
		}

		/// <summary>
		/// Get the related 'EmployeeTerritory' DTO list
		/// </summary>
		public static EmployeeTerritoryDTO[] GetEmployeeTerritoryList(this EmployeeDTO item)
		{
			var proxy = ProxyFactory<EmployeeDTO>.GetProxy();
			IEmployeeService service = (IEmployeeService)proxy;
			return service.GetEmployeeTerritoryList(item).ToArray();
		}

		/// <summary>
		/// Get the related 'Order' DTO list
		/// </summary>
		public static OrderDTO[] GetOrderList(this EmployeeDTO item)
		{
			var proxy = ProxyFactory<EmployeeDTO>.GetProxy();
			IEmployeeService service = (IEmployeeService)proxy;
			return service.GetOrderList(item).ToArray();
		}


		#endregion

		#region Delete

		/// <summary>
		/// Delete a single object.
		/// </summary>
		public static void Delete(this EmployeeDTO employee)
		{
			var proxy = ProxyFactory<EmployeeDTO>.GetProxy();
			IEmployeeService employeeService = (IEmployeeService)proxy;
			employeeService.Delete(employee);
		}

		#endregion

		/// <summary>
		/// Clones one DTO to another.
		/// </summary>
		internal static void CopyObject(EmployeeDTO source, EmployeeDTO destination)
		{
			destination.TitleOfCourtesy = source.TitleOfCourtesy;
			destination.Title = source.Title;
			destination.ReportsTo = source.ReportsTo;
			destination.Region = source.Region;
			destination.PostalCode = source.PostalCode;
			destination.PhotoPath = source.PhotoPath;
			destination.Photo = source.Photo;
			destination.Notes = source.Notes;
			destination.LastName = source.LastName;
			destination.HomePhone = source.HomePhone;
			destination.HireDate = source.HireDate;
			destination.FirstName = source.FirstName;
			destination.Extension = source.Extension;
			destination.EmployeeId = source.EmployeeId;
			destination.Country = source.Country;
			destination.City = source.City;
			destination.BirthDate = source.BirthDate;
			destination.Address = source.Address;
		}

	}
}
